import Schema, { type Rules } from "async-validator";
import { ElMessage } from "element-plus";

/**
 * 校验视频表单数据
 * @param formData 表单数据
 * @param rules async-validator 的规则
 * @param index 可选，用于提示是第几个（从 0 开始）
 */
export async function validateVideoForm(
  formData: any,
  rules: Rules,
  index?: number
): Promise<boolean> {
  const validator = new Schema(rules);

  try {
    await validator.validate(formData);
    return true;
  } catch (err: any) {
    const msg = err?.errors?.[0]?.message || "表单校验失败";
    if (typeof index === "number") {
      ElMessage.error(`第 ${index + 1} 个视频：${msg}`);
    } else {
      ElMessage.error(msg);
    }
    return false;
  }
}
